﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SJ.Platform.Common
{
    /// <summary>
    /// 日期操作类
    /// </summary>
    public class DateTimeHandle
    {
        public static List<DateTime>? holidays { get; set; }
        /// <summary>
        /// 计算两个日期之间的工作日
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="holidays"></param>
        /// <returns></returns>
        public static int CalculateBusinessDays(DateTime startDate, DateTime endDate)
        {
            if (startDate > endDate)
            {
                return 0;
            }

            // 计算总天数
            int totalDays = (endDate - startDate).Days + 1;

            int businessDays = 0;

            for (int i = 0; i < totalDays; i++)
            {
                DateTime currentDate = startDate.AddDays(i);
                if (IsBusinessDay(currentDate))
                {
                    businessDays++;
                }
            }

            return businessDays;
        }

        /// <summary>
        /// 是否是工作日
        /// </summary>
        /// <param name="date"></param>
        /// <param name="holidays"></param>
        /// <returns></returns>
        public static bool IsBusinessDay(DateTime date)
        {
            // 检查是否是周末
            if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
            {
                return false;
            }

            // 检查是否是法定节假日
            if (holidays != null && holidays.Count > 0 && holidays.Contains(date.Date))
            {
                return false;
            }

            return true;
        }
    }
}
